﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangMiEntity;
using WangMiEntity.Menu;
using CommonFramework;

namespace WangMiDAL
{
    public class MenuDAL
    {
        public static List<MenuEntity> GetMainMenu()
        {
            var db = new Database("sqlserver");
            return db.Query<MenuEntity>("select * from Menu where ParentId is null").ToList<MenuEntity>();
        }

        /// <summary>
        /// 获取左菜单列表
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static MenuModel GetLeftMenu(string menuId)
        {
            var db = new Database("sqlserver");
            MenuModel menuModel = new MenuModel();
            List<LeftMenuModel> leftMenuList = new List<LeftMenuModel>();

            var leftMenu = db.Query<MenuEntity>("select * from Menu where ParentId =@ParentId", new { ParentId = menuId }).ToList<MenuEntity>();

            foreach (var item in leftMenu)
            {
                LeftMenuModel LeftMenuModel = new LeftMenuModel()
                {
                    MenuId = item.MenuId,
                    MenuLink = item.MenuLink,
                    MenuName = item.MenuName,
                    ParentId = item.ParentId
                };

                List<MenuEntity> menuList = new List<MenuEntity>();
                db.Query<MenuEntity>("select * from Menu where ParentId =@ParentId", new { ParentId = item.MenuId }).ToList<MenuEntity>().ForEach(m =>
                {
                    MenuEntity menuEntity = new MenuEntity()
                    {
                        MenuId = m.MenuId,
                        MenuLink = m.MenuLink,
                        MenuName = m.MenuName,
                        ParentId = m.ParentId
                    };

                    menuList.Add(menuEntity);
                });

                LeftMenuModel.Menu = menuList;
                leftMenuList.Add(LeftMenuModel);
            }

            menuModel.LeftMenu = leftMenuList;
            return menuModel;
        }
    }
}
